package cn.duckerkj.base.handle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.duckerkj.base.enums.ResultEnums;
import cn.duckerkj.base.exception.MyException;
import cn.duckerkj.pojo.Result;
import cn.duckerkj.util.ResultUtil;

@ControllerAdvice
@Component
public class ExceptionHandle {
	
	private final static Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);
	
	@ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result handle(Exception e) {
		logger.error("exceptionmsg="+e.getMessage());
        if (e instanceof MyException) {
        	MyException girlException = (MyException) e;
            return ResultUtil.error(girlException.getCode(), girlException.getMessage());
        }else {
            logger.error("【系统异常】{}", e);
            return ResultUtil.error(ResultEnums.UNKONW_ERROR.getCode(),ResultEnums.UNKONW_ERROR.getMsg());
        }
    }
	
}
